<template>
  <div class="dropdown" ref="dropdownRef">
    <a href="#" class="btn btn-outline-primary my-2 dropdown-toggle" @click.prevent="toggleOpen">{{ title }}</a>
    <ul class="dropdown-menu" :style="{display: 'block'}" v-if="isOpen">
        <slot></slot>
    </ul>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import useClickOutside from '../hooks/useClickOutside'

defineProps({
    title: {
        type: String,
        required: true
    }
})

const dropdownRef = ref<null | HTMLElement>(null)
const isOpen = ref(false)
const toggleOpen = () => {
    isOpen.value = !isOpen.value
}
const isClickOutside = useClickOutside(dropdownRef)
watch(isClickOutside, (isClickOutside) => {
    if(isClickOutside && isOpen.value) {
        isOpen.value = false
    }
})
</script>